
# year_strings = [
#     '185001010600-187001010000',
#     '187001010600-189001010000',
#     '189001010600-191001010000',
#     '191001010600-193001010000',
#     '193001010600-195001010000',
#     '195001010600-197001010000',
#     '197001010600-199001010000',
#     '199001010600-201001010000',
#     '201001010600-201501010000'
# ]
if config['output_type'] == '6hrPlev':
    year_strings = [f'{y}01010300-{y+4}12312100' for y in range(1850, 2015, 5)]
elif config['output_type'] == 'E3hr':
    year_strings = [f'{y}01010130-{y+4}12312230' for y in range(1850, 2015, 5)]
else:
    year_strings = [f'{y}01010600-{y+5}01010000' for y in range(1850, 2015, 5)]

print(config)


#v20190815
rule download:
    output:
          "{dataset}/raw/{name}/{name}_{year_str}_raw.nc",
    shell:
         "wget {config[server_prefix]}/MPI-M/MPI-ESM1-2-HR/historical/{config["
         "run]}/{config[output_type]}/"
         "{config[cmip_name]}/gn/{config[version]}/"
         "{config[cmip_name]}_{config[output_type]}_MPI-ESM1-2-HR_historical_{config[run]}_gn_{wildcards.year_str}.nc "
         "-O {wildcards.dataset}/raw/{config[name]}/{config[name]}_{wildcards.year_str}_raw.nc"
         
        #  http://esgf-data1.llnl.gov/thredds/fileServer/css03_data/CMIP6/CMIP/MPI-M/MPI-ESM1-2-HR/historical/r1i1p1f1/6hrPlevPt/tas/gn/v20190815/tas_6hrPlevPt_MPI-ESM1-2-HR_historical_r1i1p1f1_gn_185001010600-185501010000.nc
         
        #  https://esgf.ceda.ac.uk/thredds/fileServer/esg_cmip6/CMIP6/CMIP/MPI-M/MPI-ESM1-2-HR/historical/r1i1p1f1/6hrPlevPt/uas/gn/v20190710/uas_6hrPlevPt_MPI-ESM1-2-HR_historical_r1i1p1f1_gn_185001010600-185501010000.nc

rule regrid:
    input:
          "{dataset}/raw/{name}/{name}_{year_str}_raw.nc"
    output:
          "{dataset}/{res}deg/{name}/{name}_{year_str}_{res}deg.nc.tmp"
    shell:
          "python ../../src/data_preprocessing/regrid.py \
            --input_fns {input} \
            --output_dir {wildcards.dataset}/{wildcards.res}deg/{wildcards.name} \
            --ddeg_out {wildcards.res} \
            --cmip 1 \
            --rename {config[cmip_name]} {config[era_name]} \
            --file_ending nc.tmp"

rule delete:
    input:
          expand("{{dataset}}/{res}deg/{{name}}/{{name}}_{{year_str}}_{res}deg.nc.tmp",
                 res=config['res']),
    output:
          expand("{{dataset}}/{res}deg/{{name}}/{{name}}_{{year_str}}_{res}deg.nc",
                 res=config['res'])
    priority: 100
    run:
          for i, o in zip(input, output):
              shell("mv {i} {o}")
        #   shell("rm {wildcards.dataset}/raw/{wildcards.name}/{wildcards.name}_{wildcards.year_str}_raw.nc"),


rule all:
    input:
         expand("{datadir}/{res}deg/{name}/{name}_{year_str}_{res}deg.nc",
                datadir=config['datadir'], res=config['res'], name=config['name'], year_str=year_strings)


